package pres.wchen.bussiness.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.bussiness.cor.model.BaseModel;
import pres.wchen.bussiness.service.SysRoleService;
import pres.wchen.bussiness.vo.SysRoleVO;
import pres.wchen.bussiness.vo.SysRolesPermissionsVO;
import pres.wchen.bussiness.service.SysRolesPermissionsService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import pres.wchen.bussiness.web.BaseController;

/**
 * @作者： wchen
 * @描述： 管理
 * @创建时间： 2018/02/17 15:02:18
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
@RestController
@RequestMapping("/sys/role/permission")
public class SysRolesPermissionsController extends BaseController {
	@Autowired
	private SysRolesPermissionsService sysRolesPermissionsService;

	@Autowired
	private SysRoleService sysRoleService;


	/**
     *  @描述: 权限批量新增
     *  @参数： [sysRolesPermissionsVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/17
     *  @修改人和其它信息:
     */
	@PostMapping(value = "/save")
	public Map save(@RequestBody Map map) {
		Long roleId = Long.valueOf(map.get("roleId").toString());
		List permIdList = (List) map.get("permissionIdList");
		//清除原有权限
		sysRolesPermissionsService.delete(roleId);

		//添加赋予的权限
		if(permIdList.size()> 0) {
			sysRolesPermissionsService.insertByBatch(map);
		}
		return super.commonSuccessResult();
	}


	/**
     *  @描述: 删除
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/17
     *  @修改人和其它信息:
     */
	@DeleteMapping(value = "/delete/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = sysRolesPermissionsService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
	 *  @描述: 权限赋予情况表
	 *  @参数： [id]
	 *  @返回值： java.util.Map
	 *  @创建人: wchen
	 *  @创建时间: 2018/2/17
	 *  @修改人和其它信息:
	 */
	@GetMapping(value = "/info/{id}")
	public ModelAndView info(@PathVariable("id") Long id) {
		Map conditionMap = new HashMap();
		conditionMap.put("roleId", id);
		BaseModel baseModel = sysRolesPermissionsService.chooseRolePermissionList(conditionMap);
		return super.dealListResult("/sys/role/perm_maintain_modify", baseModel);
	}


	/**
     *  @描述: 分页列表
     *  @参数： [request]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/17
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/pageList")
	public ModelAndView pageList(HttpServletRequest request) {
		Object pageSize = request.getParameter("pageSize");
		Object pageNo = request.getParameter("pageNo");
		Object roleName = request.getParameter("roleName");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("roleName", roleName);
		conditionMap.put("pageFlag", pageFlag);

		BaseModel baseModel = sysRoleService.list(conditionMap);
//		BaseModel baseModel = sysRolesPermissionsService.list(conditionMap);
		return super.dealListResult("/sys/role/perm_maintain", baseModel);
	}

}
